package com.guli.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.R;
import com.guli.eduservice.bean.EduComment;
import com.guli.eduservice.bean.UcenterMember;
import com.guli.eduservice.client.UcentClient;
import com.guli.eduservice.service.EduCommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.PseudoColumnUsage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/eduservice/educomment")
@CrossOrigin
public class EduCommentController {
    @Resource
    private EduCommentService eduCommentService;

    @Resource
    private UcentClient ucentClient;

    //根据课程id查询评论列表
    @GetMapping("/getCommentById/{page}/{limit}")
    public R getCommentById(@PathVariable("page")long page,@PathVariable("limit")long limit
            ,@RequestParam("courseId")String courseId){
        Map<String,Object> map = eduCommentService.getCommentById(courseId,page,limit);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/addComment")
    public R save(@RequestBody EduComment comment, @RequestHeader("token")String token) {
        String memberId = JwtUtils.getMemberIdByJwtToken2(token);
        if(StringUtils.isEmpty(memberId)) {
            return R.error().code(20001).message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMember ucenterInfo = ucentClient.getMemberByTokenToComment(token);
        System.out.println(ucenterInfo);
        comment.setNickname(ucenterInfo.getNickname());
        comment.setAvatar(ucenterInfo.getAvatar());
        eduCommentService.save(comment);
        return R.ok();
    }
}

